package com.wdzl.frame;

import com.wdzl.util.ImageUtil;
import com.wdzl.util.MouseAdapterFrame;
import com.wdzl.util.MyPlayer;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 1.新建窗体 继承 JFrame
 */
public class MainFrame extends MouseAdapterFrame {
    //构造方法
    public MainFrame(){
        //设置标题
        setTitle("羊了个羊游戏");

        //设置窗体大小
        setSize(463,830);
        //设置绝对布局
        setLayout(null);
        //禁止改变大小
        setResizable(false);
        //设置关闭窗体时，直接结束程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //设置窗体显示位置
        setLocationRelativeTo(null);

        //绘制区域1
        initRightArea1();
        initLeftArea1();
        initLeftArea2();
        initRightArea2();

        initTowerArea();

        //调用初始化背景
        initBackground();

        // 显示窗体
        setVisible(true);

        new Thread(()->{
            MyPlayer.playMusic();}).start();
    }

    /**
     * 小目标：背景和卡槽绘制
     * 1.图片加载
     * 2.添加到窗体中显示
     */

    private void initBackground(){
        //JLabel 组件 ----文本或图像

        ImageIcon bgImg = ImageUtil.getImageIconByImgs("背景草地");
        ImageIcon cardAreaImg = ImageUtil.getImageIconByImgs("消除区域");

        // 绘制卡槽
        Card cardArea = new Card(cardAreaImg);
        cardArea.setSize(cardAreaImg.getIconWidth(),cardAreaImg.getIconHeight());
        cardArea.setLocation(12,getHeight()-cardArea.getHeight()-50);
        add(cardArea);

        //把图片对象交给JLabel
        JLabel bgLbl = new JLabel(bgImg);
        bgLbl.setSize(bgImg.getIconWidth(),bgImg.getIconHeight());
        bgLbl.setLocation(0,0);
        //添加到窗体中
        add(bgLbl);


    }
    /**
     * 绘制塔形区域
     */
    private void initTowerArea(){
        System.out.println("======TowerList:"+CardModel.towerList.size());
        int index = 0;
        for (int i = 1; i <= 5; i++) {
            // 下面绘制每一层n*n的卡片
            for (int x = 0; x < i; x++) {
                for (int y = 0; y < i; y++) {
                    Card card = CardModel.towerList.get(index);
                    card.setLocation(200-(i-1)*25+x*50,150-(i-1)*25+y*50);
                    System.out.println(">>>>>=="+card.getName());
                    add(card);
                    index++;
                }
            }
        }
    }
    /**
     * 绘制区域2卡牌
     */
    public void initRightArea2(){
        List<Card> list = CardModel.rightArea2List;
        addCardToPanel(list,150,360,-5,0);
    }
    /**
     * 绘制区域2卡牌
     */
    public void initLeftArea2(){
        List<Card> list = CardModel.leftArea2List;
        addCardToPanel(list,280,360,5,0);
    }
    /**
     * 绘制区域1卡牌
     */
    public void initRightArea1(){
        List<Card> list = CardModel.rightArea1List;
        addCardToPanel(list,100,460,0,6);
    }

    public void initLeftArea1(){
        List<Card> list = CardModel.leftArea1List;
//        for (int i = 0; i < list.size(); i++) {
//            Card card = list.get(i);
//            card.setLocation(100,400+i*10);
//            add(card);
//            card.addMouseListener(this);
//        }

        addCardToPanel(list,300,460,0,6);
    }

    /**
     * 添加卡牌到窗体面板界面中
     */
    private void addCardToPanel(List<Card> list,int initX,int initY,int x0,int y0){
        for (int i = 0; i < list.size(); i++) {
            Card card = list.get(i);
            card.setLocation(initX+i*x0,initY+i*y0);
            add(card);
            card.addMouseListener(this);
        }
    }


    /**
     * 当鼠标按下时
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getSource() instanceof Card){
            Card card = (Card) e.getSource();
//            card.setVisible(false);
            //放入选中的卡槽池中去
            CardModel.selectedList.add(card);

            sameThreeRemove(card);
        }

        drawCardPool();

        repaint(); //重新刷新界面

    }
    /**
     * 判断是否有相同的，然后消卡移除
     */
    private void sameThreeRemove(Card card){
        String name = card.getName();
        // 存放相同的卡，检查是否达到消除的数量
        List<Card> sameCards = new ArrayList<>();
        for (int i = 0; i < CardModel.selectedList.size(); i++) {
            Card c = CardModel.selectedList.get(i);
            //如果是相同图案的卡片
            if(c.getName().equals(name)){
                sameCards.add(c);
            }
        }
        System.out.println(">>>size():>>"+sameCards.size());
        if(sameCards.size()>=3){
            for (Card sameCard : sameCards) {
                sameCard.setVisible(false);
                CardModel.selectedList.remove(sameCard);
                System.out.println("========="+CardModel.selectedList.size());
            }
        }
    }
    /**
     * 绘制卡槽池中卡牌
     */
    public void drawCardPool(){
        System.out.println(">>>>drawCardPool>==="+CardModel.selectedList.size());
        for (int i = 0; i < CardModel.selectedList.size(); i++) {
            Card card = CardModel.selectedList.get(i);
            card.setLocation(33+i*56,702);
        }
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}
